function X_filtered = ecg_smooth2(X,Order,Windows,Samp_freq)
% ECG_SMOOTH2       Smoothing filter for ECG (Version 2)
% Usage:
% X_filtered = ecg_smooth(X);
% X_filtered = ecg_smooth(X, Order);
% X_filtered = ecg_smooth(X, Order, Windows);
% X_filtered = ecg_smooth(X, Order, Windows, Samp_freq);
%
% Inputs:
% X: Discrete time signal
% Order: Order of fitting polynomial (<Window)
% Window: window size (odd no.)
% 
% Outputs:
% X_filtered: filtered signal
%
%
% Difference from Version 1:
% Windows and order become smaller for coarse smoothing curve
%
% Debug mode:
% If output variable is not specified, debug mode will ...
% automatically turned on.
%
% Filtered signal and Frequencncy response of ...
% the notch filter will be drawn in new figure window.
%
% See also: sgolayfilt
% Modified at 27th Oct 2010
% Copyright by Group 8
%-------------------------------------------------------------------------
if(nargin < 4)
    Samp_freq=550;
end
if(nargin < 3)
    Windows = 43;
end
if(nargin < 2)
    Order=2;
end

% Savitzky-Golay filtering
    X_filtered = sgolayfilt(X, Order, Windows);

%Debug mode:
if(~nargout)
t = (0:length(X)-1)/Samp_freq;
subplot(2,1,1), plot(t,X,';Raw signal;'),xlim([15 25])
ylabel('Voltage / V')
subplot(2,1,2), plot(t,X_filtered,';Filtered signal;'),xlim([15 25])
xlabel('time / s');
end




